<%@page import="service.Link"%>
<%@page import="service.PutCache"%>
<%@page import="model.NewsRecent"%>
<%@page import="java.util.ArrayList"%>
<%@page import="javax.cache.CacheException"%>
<%@page import="java.util.Collections"%>
<%@page import="javax.cache.CacheManager"%>
<%@page import="javax.cache.Cache"%>
<%@page import="service.Util"%>
<%@page import="model.Tags"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.text.Format"%>
<%@page import="model.News"%>
<%@page import="com.googlecode.objectify.Query"%>
<%
	Tags tag = (Tags) request.getAttribute("tag");
	response.setCharacterEncoding("utf-8");
	Format formatter = new SimpleDateFormat("dd-MM-yyyy h:m:s");
	
	ArrayList<String> newTag = PutCache.getCacheList("newTag");
	
	ArrayList<NewsRecent> newRecent = PutCache.getCacheNews("newRecent");
	
	ArrayList<Query<News>> listCate = (ArrayList<Query<News>>) request.getAttribute("listCate");
	
	int pages = Integer.parseInt((String) request.getAttribute("page"));
	
	String url = Link.linkTag()+"/"+tag.getAlias();
	int k=0;
	String title = "Tìm kiếm "+tag.getName();
	String description = "Tìm kiếm tin tức thuộc cùng từ khóa "+tag.getName();
	String keyword = tag.getName()+",tin tức,tin hót,tin mới nhất,tổng hợp tin";
	request.setCharacterEncoding("UTF-8");
	response.setContentType("text/html; charset=utf-8");
	
	ArrayList<String> category = PutCache.getCacheList("category");
%>
<%@ page language="java" import="java.util.Enumeration"
	pageEncoding="utf-8" contentType="text/html;charset=utf-8"%>
<jsp:include page="layout/header.jsp">
	<jsp:param name="title" value="<%=title %>" />
	<jsp:param name="description" value="<%=description %>" />
	<jsp:param name="keywords" value="<%=keyword %>" />
</jsp:include>

<div class="body">
	<div class="body-left">
		<ul class="highlight">
			<li class="tg"><h1><%=tag.getName() %></h1></li>
			<%
				if (tag != null && tag.getListNews().size() > 0) 
				{
					ArrayList<String> listNew = Util.awayArrayList(tag.getListNews());
					
					int start = (pages-1)*20;
					int end = (pages-1)*20 + 20;
					if(end > tag.getListNews().size())
					{
						end = tag.getListNews().size();
					}
					for(int i=start;i<end;i++)
					{
						k++;
						String[] arr = listNew.get(i).split("\\|");
						if(i==start)
						{
			%>
							<li class="clearfix first"><a href="/<%=Link.linkDetail() %>/<%=Util.convertAlias(arr[0])%>">
									<img alt="<%=Util.cleanString(arr[0]) %>" src="<%=arr[3]%>">
							</a>
								<h3>
									<a href="/<%=Link.linkDetail() %>/<%=Util.convertAlias(arr[0])%>"><%=arr[0]%></a>
								</h3> <span><%=formatter.format(Long.parseLong(arr[2])) %></span>
								<p><%=arr[1]%></p> <a
								class="more" href="/<%=Link.linkDetail() %>/<%=Util.convertAlias(listNew.get(i))%>">Xem tiếp</a></li>
			<%
						} else {
			%>
							<li class="clearfix "><a href="/<%=Link.linkDetail() %>/<%=Util.convertAlias(arr[0])%>">
									<img alt="<%=Util.cleanString(arr[0]) %>" src="<%=arr[3]%>">
							</a>
								<h3>
									<a href="/<%=Link.linkDetail() %>/<%=Util.convertAlias(arr[0])%>"><%=arr[0]%></a>
								</h3> <span><%=formatter.format(Long.parseLong(arr[2]))%></span>
								<p><%=arr[1]%></p> <a
								class="more" href="/<%=Link.linkDetail() %>/<%=Util.convertAlias(arr[0])%>">Xem tiếp</a></li>
			<%
						}	
					}
				}
			%>
			<li> 
				<div class="paging">
					<jsp:include page="paginate.jsp">
						<jsp:param name="cur_page" value="<%=pages %>"/>
						<jsp:param name="url" value="<%=url %>"/>
						<jsp:param name="number_game" value="<%=k %>"/>
					</jsp:include>
				</div>
			</li>
		</ul>
	</div>
	<div class="body-right">
		<ul class="mostview clearfix">
			<li class="head">Xem Mới nhất</li>
			<%
			if(newRecent.size()>0)
			{
				for(int i=0;i<newRecent.size();i++)
				{
			%>
				<li>
					<a class="avatar" href="/<%=Link.linkDetail() %>/<%=Util.convertAlias(newRecent.get(i).getTitle())%>">
						<img alt="<%=Util.cleanString(newRecent.get(i).getTitle()) %>" src="<%=newRecent.get(i).getThumb()%>">
					</a>
					<h3>
						<a href="/<%=Link.linkDetail() %>/<%=Util.convertAlias(newRecent.get(i).getTitle())%>"><%=newRecent.get(i).getTitle() %></a>
					</h3> <span><%=formatter.format(newRecent.get(i).getDate()) %></span></li>
			<%
				}
			}
			%>
		</ul>
		<div class="focus">
			<%
			if(category.size()>0)
			{
				for(int i=0;i<category.size();i++)
				{
					if(i%2==0)
					{
			%>
					<div class="focus-left fl">
						<%
						ArrayList<NewsRecent> cacheCcategory = PutCache.getCacheNews(category.get(i));
						
						for(int j=(cacheCcategory.size()-1);j>=0;j--)
						{
							if(j==(cacheCcategory.size()-1))
							{
						%>
								<h6>
									<a href="/<%=Link.linkCate() %>/<%=category.get(i)%>"><%=cacheCcategory.get(j).getCategory()%></a>
								</h6>
								<div class="clearfix">
									<a class="avatar" href="/<%=Link.linkDetail() %>/<%=Util.convertAlias(cacheCcategory.get(j).getTitle())%>">
										<img alt="<%=Util.cleanString(cacheCcategory.get(j).getTitle()) %>" src="<%=cacheCcategory.get(j).getThumb()%>">
									</a>
									<h3>
										<a href="/<%=Link.linkDetail() %>/<%=Util.convertAlias(cacheCcategory.get(j).getTitle())%>"><%=cacheCcategory.get(j).getTitle() %></a>
									</h3>
								</div>
								<ul>
							<%
							} else {
							%>
							
								<li><a href="/<%=Link.linkDetail() %>/<%=Util.convertAlias(cacheCcategory.get(j).getTitle())%>"><%=cacheCcategory.get(j).getTitle() %></a></li>
							<%
							}
						}
						%>
						</ul>
					</div>
					<%
					} else {
					%>
						<div class="focus-right fr">
							<%
							ArrayList<NewsRecent> cacheCcategory = PutCache.getCacheNews(category.get(i));
							
							for(int j=(cacheCcategory.size()-1);j>=0;j--)
							{
								if(j==(cacheCcategory.size()-1))
								{
							%>
							<h6>
								<a href="/<%=Link.linkCate() %>/<%=category.get(i)%>"><%=cacheCcategory.get(j).getCategory()%></a>
							</h6>
							<div class="clearfix">
								<a class="avatar" href="/<%=Link.linkDetail() %>/<%=Util.convertAlias(cacheCcategory.get(j).getTitle())%>">
										<img alt="<%=Util.cleanString(cacheCcategory.get(j).getTitle()) %>" src="<%=cacheCcategory.get(j).getThumb()%>">
								</a>
								<h3>
									<a href="/<%=Link.linkDetail() %>/<%=Util.convertAlias(cacheCcategory.get(j).getTitle())%>"><%=cacheCcategory.get(j).getTitle() %></a>
								</h3>
								<span><%=formatter.format(cacheCcategory.get(j).getDate()) %></span>
							</div>
							<p>- <%=cacheCcategory.get(j).getDes() %></p>
							<ul>
							<%
							} else {
							%>
								<li><a href="/<%=Link.linkDetail() %>/<%=Util.convertAlias(cacheCcategory.get(j).getTitle())%>"><%=cacheCcategory.get(j).getTitle() %></a></li>
							<%
							}
						}
						%>
						</ul>
					</div>
					<div class="clear"></div>
		<%
					}
				}
			}
		%>
			<div class="clear"></div>
		</div>

		<div class="cloudtags">
			<span>Có thể bạn quan tâm</span>
			<ul class="clearfix">
				<%
				if(newTag.size()>0)
				{
					for(int i=0;i<newTag.size();i++)
					{
				%>
						<li><a href="/<%=Link.linkTag() %>/<%=Util.convertAlias(newTag.get(i))%>"> <%=newTag.get(i) %> </a></li>
				<%
					}
				}
				%>
			</ul>
		</div>
	</div>
	<div class="clear"></div>
</div>

<jsp:include page="layout/footer.jsp"></jsp:include>